package thread_safe;

/**
 * @Description: 懒汉式中线程安全， 使用synchronized 方法保证线程安全
 * @Author: WangBlue
 * @Create: 2024-09-23 14:51
 * @Version: 1.0
 **/

public class ThreadSafe {
    // 构造器私有化
    public ThreadSafe() { }

    // 单例模式
    private static ThreadSafe instance = null;

    public static synchronized ThreadSafe getInstance() {
        if (instance == null) {
            instance = new ThreadSafe();
        }
        return instance;
    }
}

class Test{
    public static void main(String[] args) {
        ThreadSafe threadSafe = ThreadSafe.getInstance();
        ThreadSafe threadSafe1 = ThreadSafe.getInstance();
        System.out.println("懒汉式中线程安全方式一");
        if (threadSafe == threadSafe1) {
            System.out.println("两个对象是同一个实例");
        }

    }
}
